package com.gsoe.judge.controller;

import com.gsoe.base.model.Result;
import com.gsoe.dto.SaveCacheDTO;
import com.gsoe.judge.service.ExamCacheService;
import com.gsoe.judge.service.TestCacheService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 测验记录缓存 前端控制器
 * </p>
 *
 * @author xiaoma
 * @since 2024-11-04
 */
@RestController
@RequestMapping("/judge/testCache")
public class TestCacheController {
    @Autowired
    TestCacheService testCacheService;


    @PostMapping("/saveInput")
    public Result<String> saveInput(@RequestBody @Valid SaveCacheDTO saveCacheDTO) {
        boolean b = testCacheService.saveInput(saveCacheDTO);
        if(!b){
            Result<String> result = new Result<>();
            result.setCode(503);
            result.setMsg("测验已结束");
            return result;
        }
        return Result.success("ok");
    }

    @GetMapping("/getTestCache/{testId}")
    public Result<Map<Long,String>> getTestCache(@PathVariable("testId") Long testId) {
        return Result.success(testCacheService.getTestCache(testId));
    }


    @DeleteMapping("/delByTextId/{id}")
    public Result<String> delByTextId(@PathVariable("id") Long id){
        testCacheService.delByTextId(id);
        return Result.success("ok");
    }
}
